from datetime import timedelta

from django.utils import timezone
from rest_framework.generics import ListAPIView
from rest_framework.response import Response

from rest_framework.views import APIView


# 视图的目的是统计用用户的总人数，不涉及校验，也不涉及模型类，所以只需要继承APIView
from apps.goods.models import GoodsVisitCount
from apps.meiduo_admin.serializers.home_serializers import GoodsVisitModelSerializer
from apps.orders.models import OrderInfo
from apps.users.models import User


class UserTotalCountView(APIView):

    def get(self, request):
        # 获取查询统计的时间和统计的人数
        count = User.objects.count()
        cur_time = timezone.localtime()
        # cur_time:  2020-09-27 00:14:25.002694+00:00
        # cur_time_date:  2020-09-27

        return Response({
            "count": count,
            "date": cur_time.date(),
        })

# 日增用户数量的统计：
class UserDayCountView(APIView):

    def get(self, request):

        cur_time = timezone.localtime()
        cur_time_0 = cur_time.replace(hour=0, minute=0, second=0, microsecond=0)
        count = User.objects.filter(date_joined__gt=cur_time_0).count()

        return Response({
            "count": count,
            "date": cur_time.date()
        })


# 日活跃用户
class UserActiveCountView(APIView):

    def get(self, request):
        cur_time = timezone.localtime()
        cur_time_0 = cur_time.replace(hour=0, minute=0, second=0, microsecond=0)
        count = User.objects.filter(last_login__gt=cur_time_0).count()

        return Response({
            "count": count,
            "date": cur_time.date()
        })


# 日下单用户统计
class UserOrderCountView(APIView):

    def get(self, request):
        cur_time = timezone.localtime()
        cur_time_0 = cur_time.replace(hour=0, minute=0, second=0, microsecond=0)

        # # 方案一：从表入手
        # orders = OrderInfo.objects.filter(
        #     create_time__gt=cur_time_0
        # )
        #
        # count = len(set(order.user for order in orders))

        #  方案二： 从主表入手：
        count = len(set(User.objects.filter(orders__create_time__gt=cur_time_0)))

        return Response({
            "count": count,
            "date": cur_time.date()
        })

# 月新增用户统计
class  UserMonthCountView(APIView):

    def get(self, request):

        end_time = timezone.localtime()
        end_time_0 = end_time.replace(hour=0, minute=0, second=0, microsecond=0)

        start_time = end_time - timedelta(days=29)

        ret_list = list()

        for index in range(30):

            # 获取某天的0时刻时间
            day_time_0 = start_time + timedelta(days=index)
            # 获取某天的第二天0时刻时间
            day_time_next_0 = day_time_0 + timedelta(days=1)

            count = User.objects.filter(date_joined__gte=day_time_0, date_joined__lt=day_time_next_0).count()

            ret_list.append({"count": count, "date": day_time_0.date()})


        return Response(ret_list)



# 获取访问量数据列表：
class GoodsDayView(ListAPIView):
    # 能不能在视图中直接查找呢？不能，因为此时的cur_0_time是类属性，他是在程序执行的时候执行，所以
    # 获取到的时间是服务器启动时的时间，而不是管理员用户查询的时间。
    # cur_0_time = timezone.localtime().replace(hour=0, minute=0, second=0)
    # queryset = GoodsVisitCount.objects.filter(
    #     create_time__gte=cur_0_time
    # )

    # 如何解决？ ---> 每次请求都需要执行get_queryset方法，所以只需要重写get_queryset方法就可以了。
    queryset = GoodsVisitCount.objects.all()
    serializer_class = GoodsVisitModelSerializer

    def get_queryset(self):
        cur_time = timezone.localtime()
        cur_time_0 = cur_time.replace(hour=0, minute=0, second=0, microsecond=0)
        return self.queryset.filter(
            create_time__gte=cur_time_0
        )














